//
//  ViewController.swift
//  YSWaterflowLayoutExample
//
//  Created by MOLBASE on 2017/12/11.
//  Copyright © 2017年 YangShen. All rights reserved.
//

import UIKit

private let kCellId = "kCellId"

class ViewController: UIViewController {

    fileprivate lazy var collectionView: UICollectionView = {
        let layout = YSWaterfallLayout()
//        let cols = 3
        let itemMargin: CGFloat = 10
//        let itemWH = (self.view.bounds.width - itemMargin * CGFloat(cols + 1)) / CGFloat(cols)
//        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
        layout.dataSource = self
        
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kCellId)
        collectionView.dataSource = self
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        } else {
             automaticallyAdjustsScrollViewInsets = false
        }
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        self.extendedLayoutIncludesOpaqueBars = true
        
        view.addSubview(collectionView)
        
    }

}

extension ViewController: YSWaterfallLayoutDataSource {
    func numberOfColumnCount(_ waterfall: YSWaterfallLayout) -> Int {
        return 3
    }
    
    func waterfall(_ waterfall: YSWaterfallLayout, item: Int) -> CGFloat {
        return CGFloat(arc4random_uniform(150) + 100)
    }
}

extension ViewController: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCellId, for: indexPath)
        cell.contentView.backgroundColor = UIColor.random
        return cell
        
    }
}



